home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 21 / recursion / on_recursion.txt < prev    next >
Encoding:
Text File  |  1994-01-27  |  7.5 KB  |  115 lines

  1.   A Summary of the Barnes-Butterfield War    In all innocence, we wrote a short
  2.      YOU CAN SO RECURSE IN AMIGA BASIC       article in Vol. III, No. 2, to de-
  3.                    AND,                      monstrate recursion for an Amigan
  4.          INSIGHTS INTO THE FOLLIES           who asked what it involved. In that
  5.          OF BRUTE FORCE RECURSION            article, we said you can't "use re-
  6.                                              cursion" in Amiga Basic. Not long
  7. after, a letter arrived, addressed to "The Hater and Vilifier of Amiga Basic, PO
  8. Box 411..." The Postmistress handed it to us and said, "Better tone down what-
  9. ever it is you're writing, Dick. We're getting tired of checking the hate-mail
  10. for letter bombs." (Most of the hate mail arrives from software houses who fail
  11. to appreciate our comments on their programs).
  12.  
  13.  But this wasn't hate mail. In his usual calm, precise way, Jim Butterfield told
  14. us we could recurse in Amiga Basic with GOSUB, and demonstrated how. He also up-
  15. held the honor and utility of that language with many a good example. Last, he
  16. warned that brute force recursion on a knight's tour of a full 8 x 8 board might
  17. require half of forever. We replied at length, expounding upon the many virtues
  18. of True Basic, and objecting that GOSUB did not allow passage of arguments dur-
  19. ing recursion. (This began as a "my language is better than your language" sort
  20. of thing, from which both of us soon had the good sense to desist.)<
  21.  
  22.  One rainy afternoon, in about two hours, we wrote a simple True Basic program
  23. to demonstrate recursion in action as the program attempted to solve a 4 x 4
  24. board. We sent it to Jim because it was short and showed graphically each recur-
  25. sive move. We also sent a copy to old master Terry Peterson in California, and
  26. asked if he could adapt it to A/C Basic (the compiled version), for, if memory
  27. served, A/C Basic could accept recursive subroutine calls with arguments.<
  28.  
  29.   Terry Peterson responded first, with a much-improved recursive version of our
  30. program, in A/C Basic, which ran about twice as fast as ours did. It showed that
  31. A/C Basic indeed recursed with efficiency and speed. Okay, Basic wins the first
  32. battle of the war. Then Jim wrote a thoughtful letter, noting the overwhelming
  33. follies of plain, brute-force recursion. Following are a few of his ideas, which
  34. we demonstrate on a 5 x 5 board:
  35.  
  36.     1  0  3  8  0       `MISSED CORNERS:`Only two ways into and out of a corner
  37.     0  13 6  0  4        exist. If you you use one to get there, you must use
  38.     0  2  9  12 7        the other to leave. At left, the moves around the upper
  39.     0  11 0  5  0        right corner ignore the rule. Move 13 could make the
  40.     0  0  0  10 0        corner, but leave no way out. When such corners don't
  41.                          fill properly, a recursive program wastes an enormous
  42. amount of time to learn that the board can't be solved until it backs off and
  43. makes the appropriate moves into and out of each corner. (Yes, a 5 x 5 board can
  44. be solved).
  45.  
  46.     1  6  3  12 9       `TRAPS at MISSED SQUARES:`At left sit two blank squares,
  47.     4  11 8  0  0        in positions 5 and 6, row 2. Move 14 made it impossible
  48.     7  2  5  10 13       for either to be reached; move 15 blocks the knight ut-
  49.     0  15 0  0  0        terly. As you watch a knight move recursively, you can
  50.     0  0  0  14 0        see these traps develop.
  51.  
  52. There are two obvious mechanical solutions: 1) develop algorithms able to fill
  53. corners with logic, and 2) to look ahead and avoid the traps at missed squares.
  54. No one, so far as we know, has written such routines. Indeed, there's no assur-
  55. rance that they'd be faster than brute force recursion.
  56.  
  57. `Jim Butterfield, with his usual ingenuity,`asked why the viewer can't intervene
  58. when these obvious problems develop. He wrote two programs in Amiga Basic which
  59. let you intervene at will, back off to a specific move, and try again. We find
  60. ourselves fascinated, as we use Jim's programs, with learning how to look at a
  61. board and back off to a move that'll unblock a board and let the knight proceed.
  62. Both are written for full 8 x 8 boards (Jim told us that REAL MEN do not horse
  63. around with 4 x 4 boards, even for demos. We've been eating quiche ever since.)<
  64.  
  65. The war was not without its lighter moments. Jim conceived a grand scheme based
  66. on symmetry. "Aha," he said, "a search to depth of 64 is a great time waster.
  67. With symmetry, could this not be reduced to 16? If I could find a pattern of 16
  68. moves that took me from upper-left to upper-right, I could iterate these moves
  69. (rotatated 90 degrees) to get to lower-right, etc. It wasn't hard to code, but
  70. I wondered why no useful solutions appeared. Then it struck me! THERE CAN BE
  71. NO POSSIBLE SOLUTION! Proof: On each move, a knight moves to a square of oppos-
  72. ite color. Each two-move pair, the knight returns to to the original color. Af-
  73. ter 16 moves, the knight must be on the same color it started. But--upper left
  74. and upper right corners have opposite colors! There is no way 16 moves can get
  75. from one to the other. Reductio ad absurdum. Groan..."
  76.  
  77. Jim adds, "Can we do a 32-move sequence that repeats, upper left to lower right?
  78. It takes more time than you might think, and again is helped by manual interven-
  79. tion."
  80.  
  81. `Doug Jones of Atlantic Highlands`also sent us a recursive tour in C; it works
  82. (swiftly!) on boards from 4 x 4 to 8 x 8. It states "no answer" to a 4 x 4 board
  83. in 16 seconds, solves a 5 x 5 board in less than 1 second, solves a 6 x 6 board
  84. in 9 minutes 45 seconds, and (suprise!) finds a solution to a 7 x 7 in 6:35. It
  85. prints a blue and white checkerboard on screen, and shows recursion by move num-
  86. ber on the board (it's pretty swift at that). We set it up this morning on our
  87. A-1000 with nothing else running, looking for a solution to an 8 x 8 board...<
  88.  
  89. `The Object of our Exercise in Recursion`is to demonstrate how it works. Those
  90. who want to see it in both source code and action will find all the programs on
  91. Amigan PD Disk 21. Terry Peterson placed the A/C version in the runtime package
  92. so that anyone can run it. The A/C Basic programs--as well as Jim's--graphically
  93. represent the board for each move; the effect of recursion is plain as the nose
  94. on an elephant. The source code for Doug Jones' C program is an excellent demo
  95. in that language, although the recursive work is graphically so swift it's al-
  96. most impossible to follow.
  97.  
  98. `Some numbers will make the weaknesses in brute force recursion come alive.`We
  99. ran both A/C Basic and True Basic on 8 x 8 boards for over half a billion iter-
  100. ations without finding a solution. With manual intervention, one can find a sol-
  101. ution to an 8 x 8 board with Jim's program and no practice in less than a hour.
  102. The weakness in a brute force approach is amply demonstrated by Doug Jones' pro-
  103. gram in C. We kicked it off on an 8 x 8 board at 11:59 on Friday morning, and at
  104. 23:59 had to shut it down--still not solved. Jim's comments about the stupidity
  105. of brute force recursion were amply demonstrated here. The move sequence at the
  106.             left appeared shortly after 1 p.m. on the lower-left hand corner of
  107.   13  4     the board. The program spent the next ten hours on recursive moves
  108.   0   19    higher than and beyond this obvious failure. In time, it would have
  109.             backtracked down the recursive road and filled the corner. But--in
  110. how many more hours (or days)? Brute force recursion on complex problems, as Jim
  111. said at the beginning, demands either clever auxiliary algorithms or manual in-
  112. tervention--or both. If you want to understand recursion and how it works, its
  113. weaknesses and strengths, you can do no better than read and run the programs
  114. issued on PD Disk 21.
  115.